package com.robot.member.service;

import com.robot.member.dao.CashWithdrawalDao;
import com.robot.member.pojo.CashWithdrawal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * withdrawal服务层
 *
 * @author
 */
@Service
public class CashWithdrawalService {

    @Autowired
    private CashWithdrawalDao cashWithdrawalDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<CashWithdrawal> findAll() {
        return cashWithdrawalDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<CashWithdrawal> findSearch(Map whereMap, int page, int size) {
        Specification<CashWithdrawal> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return cashWithdrawalDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<CashWithdrawal> findSearch(Map whereMap) {
        Specification<CashWithdrawal> specification = createSpecification(whereMap);
        return cashWithdrawalDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public CashWithdrawal findById(String id) {
        return cashWithdrawalDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param withdrawal
     */
    public void add(CashWithdrawal withdrawal) {
        //雪花分布式ID生成器
        withdrawal.setId(idWorker.nextId() + "");
        cashWithdrawalDao.save(withdrawal);
    }

    /**
     * 修改
     *
     * @param withdrawal
     */
    public void update(CashWithdrawal withdrawal) {
        cashWithdrawalDao.save(withdrawal);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        cashWithdrawalDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<CashWithdrawal> createSpecification(Map searchMap) {
        return new Specification<CashWithdrawal>() {
            @Override
            public Predicate toPredicate(Root<CashWithdrawal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 提现到哪里 10-微信,20-支付宝,30-银联,40-其他
                String payment = "payment";
                if (searchMap.get(payment) != null && !"".equals(searchMap.get(payment))) {
                    predicateList.add(cb.like(root.get(payment).as(String.class), "%" + (String) searchMap.get(payment) + "%"));
                }
                // 状态 0-提现中 1-提现成功 2-提现失败
                String status = "status";
                if (searchMap.get(status) != null && !"".equals(searchMap.get(status))) {
                    predicateList.add(cb.like(root.get(status).as(String.class), "%" + (String) searchMap.get(status) + "%"));
                }
                // 备注
                String remark = "remark";
                if (searchMap.get(remark) != null && !"".equals(searchMap.get(remark))) {
                    predicateList.add(cb.like(root.get(remark).as(String.class), "%" + (String) searchMap.get(remark) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 查询会员名下的所有提现记录
     *
     * @param memberId
     * @return
     */
    public Page<CashWithdrawal> findAllByMemberIdOrderByCreatedAtDesc(String memberId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return cashWithdrawalDao.findAllByMemberIdOrderByCreatedAtDesc(memberId, pageable);
    }
}
